﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Tour.BLL;
using Tour.DAL;

public partial class Modules_Menu_MenuAE : VST.Controls.BaseLCGPage
{
    public int? MenuId { get; set; }
    public int? MenuTypeId { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        MenuId = CommonHelper.QueryStringInt("MenuId", null);
        MenuTypeId = CommonHelper.QueryStringInt("MenuTypeId", null);
        if (!MenuTypeId.HasValue)
        {
            Response.Redirect("MenuTypeList.aspx");
        }
        if (!IsPostBack)
        {
            LoadDataDLL();
            LoadData();

        }
    }
    void LoadData()
    {
        if (MenuId.HasValue)
        {
            TblMenuEntity item = TblMenuManager.CreateInstant().SelectOne(MenuId.Value);
            txtName.Text = item.Name;
            txtUrl.Text = item.Url;
            ddlParent.SelectedValue = item.ParentId.ToString();
            chkVisible.Checked = item.IsVisible;
        }
    }
    void LoadDataDLL()
    {
        ddlParent.DataSource = TblMenuManager.CreateInstant().SelectTblMenuForDDL(null, WorkingLanguage, MenuTypeId.Value);
        ddlParent.DataTextField = "Name";
        ddlParent.DataValueField = "Id";
        ddlParent.DataBind();
    }
    TblMenuEntity GetTblMenu()
    {
        TblMenuEntity item = new TblMenuEntity();
        if (MenuId.HasValue)
        {
            item = TblMenuManager.CreateInstant().SelectOne(MenuId.Value);
            if (ddlParent.SelectedValue.ToInt() != item.ParentId)
            {
                UpdateOrderOffAndDelete(item.Id.ToString(), item.ParentId.ToString(), MenuTypeId.Value.ToString(), 100);
            }
            item.UpdatedBy = User.Identity.Name;
            item.UpdatedDate = DateTime.Now;
        }
        else
        {
            item.DisplayOrder = 100;
            item.CreatedBy = User.Identity.Name;
            item.CreatedDate = DateTime.Now;
            item.UpdatedBy = User.Identity.Name;
            item.UpdatedDate = DateTime.Now;
        }
        item.MenuTypeId = MenuTypeId.Value;
        item.LanguageId = WorkingLanguage;
        item.IsVisible = chkVisible.Checked;
        item.Name = txtName.Text;
        item.ParentId = ddlParent.SelectedValue.ToInt();
        item.Url = txtUrl.Text;
        return item;
    }

    protected void btnSave_Click(object sender, EventArgs e)
    {
        TblMenuManager.CreateInstant().InsertUpdate(GetTblMenu());
        TblMenuManager.CreateInstant().SortTblMenu(int.Parse(ddlParent.SelectedValue.ToString()), MenuTypeId.Value);
        Response.Redirect("MenuList.aspx?MenuTypeId=" + MenuTypeId.Value);
    }
    private void UpdateOrderOffAndDelete(string strId, string strParentId, string MenuType, int NewIndex)
    {
        string Whereclause = "";
        if (!strParentId.Equals(""))
        {
            Whereclause = String.Format(" And ParentId={0} And MenuTypeId={1}", strParentId, MenuTypeId);
        }
        string Whereclause1 = String.Format(" ID={0}", strId);
        string Whereclause2 = String.Format(" And Id<>{0}", strId);
        Tools.SqlHelper.UpdateOrders("TblMenu", Whereclause, Whereclause1, Whereclause2, NewIndex);
    }
}